/**
 * 
 */
package com.nari.basic.validform;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.basic.model.IUser;
import com.nari.basic.service.IRoleService;
import com.nari.basic.service.IUserService;
import com.nari.basic.utils.sysmgr.UserRoleUtil;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.components.validform.CallbackMsg;
import com.nari.platform.security.MD5Encoder;
import com.opensymphony.xwork2.Action;

/**
 * 基于Validform_v5.3.2的表单异步验证
 * @author Xiaopeng
 * @date 2014-7-18
 */
public class FormAsyncValidateAction extends JSONActionSupport {
	private static final long serialVersionUID = -8770617197401713665L;
	private IUserService iUserService;
	private IRoleService iRoleService;
	//文本框的name属性
	private String name;
	//文本框的值
	private String param;
	//实体的唯一标识
	private String entityUuid;
	
	/**
	 * 验证用户名的唯一性
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-7-18
	 */
	public String validUserIDUnique() throws Exception {
		//统计相同用户名的个数
		int counter = iUserService.countUsersByUserID(param, entityUuid);
		//构造响应信息
		CallbackMsg cmsg = CallbackMsg.getInstance(true, "This User Name. validate!");
		if(counter > 0) {
			cmsg = CallbackMsg.getInstance(false, "This User Name exists,Please enter another!");
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(cmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 检查登录用户输入的原密码是否正确
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-7-21
	 */
	public String validUserPassword() throws Exception {
		//录入的原密码
		String password = MD5Encoder.getMD5String(this.param);
		//系统登录用户
		IUser loginUser = UserRoleUtil.getSysLoginUser();
		//构造响应信息
		CallbackMsg cmsg = CallbackMsg.getInstance(true, "Password validate！");
		if(!loginUser.getPassword().equals(password)) {
			cmsg = CallbackMsg.getInstance(false, "Password wrong,if you want to reset password,please contact administrator!");
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(cmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 检查角色编号的唯一性
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-7-21
	 */
	public String validRoleIDUnique() throws Exception {
		//统计相同角色编号的个数
		int counter = iRoleService.countRolesByRoleID(param, entityUuid);
		//构造响应信息
		CallbackMsg cmsg = CallbackMsg.getInstance(true, "This Role No. validate!");
		if(counter > 0) {
			cmsg = CallbackMsg.getInstance(false, "This Role No. exists,Please enter another!");
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(cmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iUserService
	 */
	@JSON(serialize=false)
	public IUserService getiUserService() {
		return iUserService;
	}

	/**
	 * @param iUserService the iUserService to set
	 */
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	/**
	 * @return the iRoleService
	 */
	@JSON(serialize=false)
	public IRoleService getiRoleService() {
		return iRoleService;
	}

	/**
	 * @param iRoleService the iRoleService to set
	 */
	public void setiRoleService(IRoleService iRoleService) {
		this.iRoleService = iRoleService;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the param
	 */
	public String getParam() {
		return param;
	}

	/**
	 * @param param the param to set
	 */
	public void setParam(String param) {
		this.param = param;
	}

	/**
	 * @return the entityUuid
	 */
	public String getEntityUuid() {
		return entityUuid;
	}

	/**
	 * @param entityUuid the entityUuid to set
	 */
	public void setEntityUuid(String entityUuid) {
		this.entityUuid = entityUuid;
	}
	
}
